package cn.hfatec.shms.web.members.ykBill.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.cache.CodeInfoCache;
import cn.hfatec.shms.constants.ShmsConstant;
import cn.hfatec.shms.enums.CodeType;
import cn.hfatec.shms.exception.NotAuthException;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.system.user.model.UserParams;
import cn.hfatec.shms.system.user.service.UserService;
import cn.hfatec.shms.web.members.customer.model.CustomerParams;
import cn.hfatec.shms.web.members.ykBill.model.YkBill;
import cn.hfatec.shms.web.members.ykBill.model.YkBillParams;
import cn.hfatec.shms.web.members.ykBill.service.YkBillService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/members/ykBill")
public class YkBillAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "YkBillService")
    private YkBillService ykBillService;

    //服务层
    @Resource(name = "UserService")
    private UserService userService;

    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(YkBillParams ykBillParams, Page page, Model model) {

        Table table = ykBillService.queryYkBillTable(ykBillParams, page);
        model.addAttribute("usersList", userService.queryUserList(new UserParams()));
        model.addAttribute("overdueTypeList", CodeInfoCache.getCodeInfo(CodeType.OVERDUE_TYPE));
        model.addAttribute("vipTypeList", CodeInfoCache.getCodeInfo(CodeType.VIP_TYPE));
        model.addAttribute("ykBillsTable", table);
        model.addAttribute("ykBillParams", ykBillParams);
        return "/members/ykBill/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(YkBillParams ykBillParams, Page page, Model model) {
        try {
            list(ykBillParams, page, model);
            model.addAttribute("overdueTypeList", CodeInfoCache.getCodeInfo(CodeType.OVERDUE_TYPE));
            model.addAttribute("vipTypeList", CodeInfoCache.getCodeInfo(CodeType.VIP_TYPE));
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/members/ykBill/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }


    /**
     * 查询列表(我的客户)
     */
    @RequestMapping("/owner")
    public String listOwner(YkBillParams ykBillParams, Page page, Model model, HttpServletRequest request) throws NotAuthException {
        User user = (User) request.getSession().getAttribute(ShmsConstant.LOGIN_USER_SESSION_NAME);
        if (user == null) {
            throw new NotAuthException("用户未授权");
        }
        model.addAttribute("user", user);
        ykBillParams.setUserLoginName(user.getUserName());
        Table table = ykBillService.queryYkBillTable(ykBillParams, page);
        model.addAttribute("ykBillsTable", table);
        model.addAttribute("ykBillParams", ykBillParams);
        model.addAttribute("usersList", userService.queryUserList(new UserParams()));
        model.addAttribute("vipTypeList", CodeInfoCache.getCodeInfo(CodeType.VIP_TYPE));
        model.addAttribute("overdueTypeList", CodeInfoCache.getCodeInfo(CodeType.OVERDUE_TYPE));
        model.addAttribute("assignStatusList", CodeInfoCache.getCodeInfo(CodeType.ASSIGN_STATUS));
        return "/members/ykBill/list";
    }
    /**
     * 查询列表(我的客户)
     */
    @RequestMapping("/vipsum")
    public String listVipsum(YkBillParams ykBillParams, Page page, Model model, HttpServletRequest request) throws NotAuthException {
        Table table = ykBillService.queryYkBillTable(ykBillParams, page);
        model.addAttribute("ykBillsTable", table);
        model.addAttribute("ykBillParams", ykBillParams);
        model.addAttribute("usersList", userService.queryUserList(new UserParams()));
        model.addAttribute("vipTypeList", CodeInfoCache.getCodeInfo(CodeType.VIP_TYPE));
        model.addAttribute("overdueTypeList", CodeInfoCache.getCodeInfo(CodeType.OVERDUE_TYPE));
        model.addAttribute("assignStatusList", CodeInfoCache.getCodeInfo(CodeType.ASSIGN_STATUS));
        return "/members/ykBill/list";
    }


    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            YkBill ykBill = new YkBill();
            model.addAttribute("ykBill", ykBill);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/members/ykBill/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            YkBill ykBill = ykBillService.getYkBill(id);
            model.addAttribute("ykBill", ykBill);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/members/ykBill/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteYkBill(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            ykBillService.deleteYkBillBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveYkBill(@Valid YkBill ykBill, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            //   ykBillService.saveYkBill(ykBill);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据YkBill保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(YkBillParams ykBillParams, HttpServletRequest request, HttpServletResponse response) {
        try {
            ykBillService.exportExcel(ykBillParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
